package com.hustzp.com.xichuangzhu.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.fragment.CategoryFragment;
import com.hustzp.com.xichuangzhu.model.CollectionKinds;
import com.hustzp.com.xichuangzhu.model.Collections;
import com.hustzp.com.xichuangzhu.poetry.CatagoryListAct;

import java.util.HashMap;
import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/27 23:51
 * @Description:
 */
public class CollectionKindsAdapter extends BaseAdapter {
    private Context context;
    private List<CollectionKinds> collectionKindsList;
    private HashMap<String, List<Collections>> collectionsHashMap;

    public CollectionKindsAdapter(Context context, List<CollectionKinds> collectionKindsList, HashMap<String, List<Collections>> collectionsHashMap) {
        this.context = context;
        this.collectionKindsList = collectionKindsList;
        this.collectionsHashMap = collectionsHashMap;
    }

    @Override
    public int getCount() {
        return collectionKindsList.size();
    }

    @Override
    public Object getItem(int position) {
        return collectionKindsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context,
                    R.layout.fragment_category_group_item, null);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindViews(position, holder);
        return convertView;
    }

    private void bindViews(int position, ViewHolder holder) {
        CollectionKinds info = (CollectionKinds) getItem(position);
        if (info != null) {
            holder.tv_category_title.setText(info.getName());
            List<Collections> collectionses = collectionsHashMap.get(info.getId());
            if (collectionses != null && collectionses.size() > 0) {
                CollectionAdapter gridViewAdapter = new CollectionAdapter(
                        context, collectionses);
                holder.gv_category_in_list.setAdapter(gridViewAdapter);
            }
        }

    }

    class ViewHolder {
        GridView gv_category_in_list;
        TextView tv_category_title;

        public void initView(View view) {
            gv_category_in_list = (GridView) view
                    .findViewById(R.id.gvCollection);
            gv_category_in_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Collections collections = (Collections) parent.getAdapter().getItem(position);
                    Intent intent = new Intent(context, CatagoryListAct.class);
                    intent.putExtra("from", CategoryFragment.class.getSimpleName());
                    intent.putExtra(Collections.class.getSimpleName(), collections);
                    context.startActivity(intent);
                }
            });
            tv_category_title = (TextView) view
                    .findViewById(R.id.tvGroup);
        }
    }
}
